package com.qipay.api.schedule;

import com.qipay.baas.model.NotifyLog;
import com.qipay.notify.NotifyLogService;
import com.qipay.sp.SpChannelRuleService;
import com.qipay.withdraw.service.WithdrawService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@EnableScheduling
public class CpJobs {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public final static long ONE_Minute = 60 * 1000;
    public final static long ONE_Second = 1000;

    @Autowired
    private NotifyLogService notifyLogService;

    @Autowired
    SpChannelRuleService spChannelRuleService;

    @Autowired
    WithdrawService withdrawService;
    @Value("${schedule.open}")
    protected Boolean scheduleOpen;
    /**
     * 通知
     */
    @Scheduled(fixedDelay = ONE_Second * 5)
    public void notifyCp() {
        if (scheduleOpen) {
            int size = 20;
            List<NotifyLog> notifyList = notifyLogService.getNotifyList(size);
            if (notifyList != null && notifyList.size() > 0) {
                notifyList.stream().forEach(p -> {
                    try {
                        logger.info("send notify:" + p.getId());
                        notifyLogService.send(p);
                    } catch (Exception ex) {
                        logger.error("send notify:" + p.getId());
                        logger.error(ex.toString());
                    }
                });
            }
        }
    }



}
